/**
 * 
 */
package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * @author Fleischhacker
 * User-Input Handling
 */
public class ClientInputReader extends Thread {
	private Client parent;
	private boolean alive = false;
	private BufferedReader in = null;
	
	public ClientInputReader(Client _parent){
		this.parent = _parent;
		this.alive = false;
		in = new BufferedReader(new InputStreamReader(System.in));
	}
	
	public void run(){
		alive = true;
		while(parent.isAlive() && alive){
			commandsToClient();
		}
		alive = false;
	}
	
	public void shutdown(){
		this.alive = false;
		//this.interrupt();
	}
	
	private void commandsToClient(){
		String read = "";
		try{
			while(!in.ready()){ //Bypass in.readLine()-Lock
				if(!alive)
					throw new Exception("servershutdown");
				this.sleep(50);
			}
			read = in.readLine();
			if(read.startsWith("!login")){
				if(read.split(" ").length != 2)
					throw new Exception("userfailure"); //Abusing Exception System; Wrong Input => Shutdown
				parent.doCommands(read);
			}
			else if(read.startsWith("!logout")){
				if(read.split(" ").length != 1)
					throw new Exception("userfailure"); //Abusing Exception System; Wrong Input => Shutdown
				parent.doCommands(read);
			}
			else if(read.startsWith("!list")){
				if(read.split(" ").length != 1)
					throw new Exception("userfailure"); //Abusing Exception System; Wrong Input => Shutdown	
				parent.doCommands(read);
			}
			else if(read.startsWith("!create")){
				if(read.split(" ").length <= 2)
					throw new Exception("userfailure"); //Abusing Exception System; Wrong Input => Shutdown
				else
					Integer.parseInt(read.split(" ")[1]); //Throws NumberFormatException if the user screws up
				parent.doCommands(read);
			}
			else if(read.startsWith("!bid")){
				if(read.split(" ").length != 3)
					throw new Exception("userfailure"); //Abusing Exception System; Wrong Input => Shutdown
				else{
					Double.parseDouble(read.split(" ")[1]); //Throws NumberFormatException if the user screws up
					Double.parseDouble(read.split(" ")[2]);
				}
				parent.doCommands(read);
			}
			else if(read.startsWith("!end")){	//End of the line, force Shutdown on both Client and ClientThreads
				//TODO: Shutdown Client
				parent.doCommands(read);
				//client.shutdown(); //not needed
				return;
			}
			else if(read.length() == 0){} //Empty Text is NO invalid command (assumption)
			else{ //Woops, invalid command. Shutdown and go!
				throw new Exception("userfailure"); //Abusing Exception System again;
			}
		}catch(IOException ioe){
			System.out.println("ERROR @MAIN.commandsToClient(Client):"+ioe.getMessage());
		}catch(NumberFormatException nfe){
			if(this.isAlive()) //Output Tweak
				System.out.println("= Please use the according format!");
			parent.shutdown();
		}catch(Exception e){
			//e.printStackTrace();
			if(e != null && e.getMessage().equalsIgnoreCase("userfailure")){ //Exception Abuse;
				if(this.isAlive()) //Output Tweak
					System.out.println("= Invalid Command!\n= Please use the given commands with their syntax!\n= Shutting down Client...");
				parent.shutdown();
			}
			else if(e != null && e.getMessage().equalsIgnoreCase("servershutdown")){ //Exception Abuse; Server shutdown
				System.out.println("= Server went offline!");
			}else
				System.out.println("ERROR @MAIN.commandsToClient(Client):"+e.getMessage());
		}
	}
}
